package cn.darkhorse.admin.controller.basic;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "系统设置")
@RestController
@RequestMapping("/basic/config")
public class QmsConfigController {
    @ApiOperation("获取截取位数")
    @RequestMapping("/getScanConfig")
    public AjaxResult getScanConfig() {
        String sql = "select enable,digits from qms_barcode_config" ;
        Map<String, Object> selected = SqlRunner.db().selectOne(sql);
        if (selected == null){
            String insert = "INSERT INTO `qms_barcode_config` (`enable`, `digits`) VALUES ('0','6')";
            SqlRunner.db().insert(insert);
            selected = SqlRunner.db().selectOne(sql);
        }

        return AjaxResult.success(selected);
    }
    @ApiOperation("修改截取位数")
    @RequestMapping("/updateSubDigits")
    public AjaxResult updateSubDigits(@RequestBody JSONObject object) {
        SqlRunner.db().update("update qms_barcode_config set digits = {0},enable = {1}",object.get("digits"),object.get("enable"));
        return AjaxResult.success();
    }

//    @ApiOperation("截取值")
//    @RequestMapping("/subValue")
//    public AjaxResult subValue(String source) {
//        String sql = "select config_value from sys_config where config_key = 'qms.config.scan'" ;
//        String digitsStr = SqlRunner.db().selectObj(sql).toString();
//        if (digitsStr!=null){
//            Integer digits = Integer.valueOf(digitsStr);
//            if (source.length()>digits){
//                source = source.substring(source.length() - digits);
//            }
//        }
//        return AjaxResult.success((Object) source);
//    }
}
